#include "stdafx.h"
#include "RectAni.h"
#include "PFMEngine.h"

RectAni::RectAni()
:m_pCurElement(NULL)
,m_bIsPlay(FALSE)
,m_nCurAniIndex(0)
,m_nAniMargin(0)
,m_nAniTick(0)
{}

RectAni::~RectAni()
{
	ClearRectElement();
}

void RectAni::Update()
{
	if( m_bShow	)
	{
		if( m_bIsPlay )
		{
			--m_nAniTick;
			if( m_nAniTick < 0 )
			{
				++m_nCurAniIndex;
				if( m_nCurAniIndex >= int(m_listRectElement.size()) )
				{
					m_bIsPlay = FALSE;
					m_nCurAniIndex = 0;
				}
				else
				{
					m_nAniTick = m_nAniMargin;
				}				
			}
		}
		if( m_nCurAniIndex < int(m_listRectElement.size()) )
		{
			GetPFMEngine().AddDrawElement(m_listRectElement[m_nCurAniIndex]);
		}
	}
}
void RectAni::ChangeSize()
{
	int nLoofCnt = int(m_listRectElement.size());
	for( int i = 0; i < nLoofCnt; ++i )
	{
		m_listRectElement[i]->SetPos(float(m_nPosX), float(m_nPosY), float(m_nWidth), float(m_nHeight), m_fTexLeft, m_fTexRight, m_fTexTop, m_fTexBottom);
	}
}

void RectAni::Create(int nPosX, int nPosY, int nWidth, int nHeight)
{
	m_nPosX = nPosX;
	m_nPosY = nPosY;
	m_nWidth = nWidth;
	m_nHeight = nHeight;
	m_bIsPlay = FALSE;
	m_nCurAniIndex = 0;
	m_nAniMargin = 0;
	m_nAniTick = 0;

	ChangeSize();
}

void RectAni::AddRectElement(RectElement* pRectElement)
{
	m_listRectElement.push_back(pRectElement);
}

void RectAni::ClearRectElement()
{
	std::vector<RectElement* >::iterator itr = m_listRectElement.begin();
	while( itr != m_listRectElement.end() )
	{
		GetPFMEngine().ReleaseRect(*itr);
		++itr;
	}	
	m_listRectElement.clear();
}

void RectAni::SetTexture(int nIndex, const char* pPath)
{
	assert(nIndex >=0 && nIndex < int(m_listRectElement.size()) );

	GetPFMEngine().ChangeRectTexture(m_listRectElement[nIndex], pPath);
}

void RectAni::PlayAni(int nAniMargin)
{
	m_bIsPlay = TRUE;
	m_nCurAniIndex = 0;
	m_nAniMargin = nAniMargin;
	m_nAniTick = nAniMargin;	
}